package com.xl98.controller;


import com.xl98.entity.*;
import com.xl98.service.GroupsMsgContentService;
import com.xl98.service.GroupsService;
import com.xl98.service.MsgContentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ls
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/msg-content")
@Api(value = "群聊信息",tags = {"群聊信息"})
public class MsgContentController {
    @Autowired
    private MsgContentService msgContentService;
    @Autowired
    private GroupsMsgContentService groupsMsgContentService;
    @Autowired
    private GroupsService groupsService;

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "保存信息")
    public Result insert(String userId, String msg, String groupsId){
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
        Groups groups = groupsService.selectByID(groupsId);
        Date date = new Date();
        String msgContentId = userId+df.format(date);

        MsgContent msgContent = new MsgContent();
        msgContent.setFromUserId(userId);
        msgContent.setMsgContent(msg);
        msgContent.setFromGroupNick(groups.getName());
        msgContent.setCreateTime(date);
        msgContent.setId(msgContentId);

        GroupsMsgContent groupsMsgContent = new GroupsMsgContent();
        groupsMsgContent.setGroupsId(groupsId);
        groupsMsgContent.setMsgContentId(msgContentId);
        if(msgContentService.insert(msgContent)&&groupsMsgContentService.insert(groupsMsgContent)){
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_INSERT);
        }
        return ResultFactory.buildFailResult(ResponseCode.FAIL_INSERT);
    }

    /*@CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除信息")
    public Result delete(String msgId, String groupsId){
        if(groupsMsgContentService.deleteByGroupsIdMsgId(groupsId,msgId)){
            return ResultFactory.buildSuccessResult(ResponseCode.SUCCESS_MESSAGE);
        }
        return ResultFactory.buildFailResult(ResponseCode.FAIL_MESSAGE);
    }*/

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByGroupsId", method = RequestMethod.POST)
    @ApiOperation(value = "群id查询信息")
    public Result selectByGroupsId(String groupsId){
        return ResultFactory.buildSuccessResult(groupsMsgContentService.selectByGroupsId(groupsId));
    }

    @CrossOrigin(allowCredentials = "true", allowedHeaders = "*")
    @ResponseBody
    @RequestMapping(value = "/selectByGroupsIdUserId", method = RequestMethod.POST)
    @ApiOperation(value = "群id用户id查询信息")
    public Result selectByGIdUId(String groupsId, String userId){
        return ResultFactory.buildSuccessResult(groupsMsgContentService.selectByGroupsIdUserId(groupsId,userId));
    }
}

